package card_discount

import (
	"bp/internal/render"
	// "bp/internal/service/param"
	"bp/internal/util"
	cardDiscountGrpc "bp/rpc/gen/mwebapi/mcard_discount"
	"context"
)

func (s *CardDiscountMWebApiImp) GetCardDiscountByOutCardID(ctx context.Context, req *cardDiscountGrpc.GetCardDiscountByOutCardIDReq) (res *cardDiscountGrpc.GetCardDiscountByOutCardIDRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("card_discount_imp/query.GetCardDiscountByOutCardID=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &cardDiscountGrpc.GetCardDiscountByOutCardIDRes{}
	q := s.dao.Q

	outCardID, err := s.VldCardDiscountByOutCardIDReq(ctx, q, req)
	if err != nil {
		return
	}
	cardDiscountPtr, err := s.entityHub.CardDiscountService.GetCardDiscountPtrByOutCardID(ctx, q, outCardID)
	if err != nil {
		return
	}
	cardDiscountID := cardDiscountPtr.ID
	itemCategoryConfPtrs, err := s.entityHub.CardDiscountService.ListItemCategoryConfigurationPtrsByCardDiscountID(ctx, q, cardDiscountID)
	if err != nil {
		return
	}
	itemConfPtrs, err := s.entityHub.CardDiscountService.ListItemConfigurationPtrsByCardDiscountID(ctx, q, cardDiscountID)
	if err != nil {
		return
	}

	res.CardDiscount = s.ConvCardDiscountPtrEntityToPb(itemCategoryConfPtrs, itemConfPtrs)(cardDiscountPtr)
	return
}
